package com.chuzhong.sf.gui;

import com.chuzhong.sf.api.admin_api;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.regex.Pattern;

/**
 * @Author 初衷.
 * @Date 2020/10/15
 **/
public class Insert_user extends JFrame{

    private static int count=0;
    private static JButton bt1;//确定按钮
    private static JLabel jl_1;//登录的版面
    private static JFrame jf_1;//登陆的框架
    private static JTextField jtext1;//用户名
    private static JPasswordField jtext2;//密码
    private static JTextField jtext3;//额度
    private static JLabel jl_admin;
    private static JLabel jl_password;
    private static JLabel jl_quota;
public Insert_user(){
    Font font =new Font("宋体", Font.PLAIN, 20);//设置字体
    jf_1=new JFrame("开设用户页面");
        jf_1.setSize(500, 400);

    jl_1=new JLabel();

    jl_admin=new JLabel("用户名:");
        jl_admin.setBounds(20, 50, 100, 50);
        jl_admin.setFont(font);

    jl_password=new JLabel("密 码:");
        jl_password.setBounds(20, 120, 60, 50);
        jl_password.setFont(font);


            jl_quota=new JLabel("额度:");
            jl_quota.setBounds(20, 190, 60, 50);
            jl_quota.setFont(font);

    bt1=new JButton("确定");         //更改成loginButton
        bt1.setBounds(90, 250, 100, 50);
        bt1.setFont(font);


    //加入文本框
    jtext1=new JTextField();
        jtext1.setBounds(150, 50, 250, 50);
        jtext1.setFont(font);

    jtext2=new JPasswordField();//密码输入框
        jtext2.setBounds(150, 120, 250, 50);
        jtext2.setFont(font);
    jtext3 = new JTextField();
    jtext3.setBounds(150, 190, 250, 50);
    jtext3.setFont(font);
        jl_1.add(jtext1);
        jl_1.add(jtext2);
        jl_1.add(jtext3);
        jl_1.add(jl_admin);
        jl_1.add(jl_password);
        jl_1.add(jl_quota);
        jl_1.add(bt1);

        jf_1.add(jl_1);
        jf_1.setVisible(true);
        jf_1.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
//        jf_1.setLocation(600,300);
        jf_1.setLocationRelativeTo(null);
    ActionListener actionListener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {

            String username = jtext1.getText();
            char[] text1 = jtext2.getPassword();
            String password = String.valueOf(text1);
            String edu = jtext3.getText();
            Pattern pattern = Pattern.compile("[0-9]*");
            boolean matches = pattern.matcher(jtext3.getText()).matches();

            if(username.equals("")==false&&jtext2.getPassword().equals("")==false&&jtext3.getText().equals("")==false){
                if(matches==true){
                    try {
                        admin_api.add_user(username,password,edu);
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }
                    JOptionPane.showMessageDialog(null, "开设账户成功！请牢记您的账号密码", "提示",JOptionPane.INFORMATION_MESSAGE);
                    jf_1.dispose();
                }else{
                    JOptionPane.showMessageDialog(null,"额度必须为数字","警告",JOptionPane.WARNING_MESSAGE);
                }

            }else{
                JOptionPane.showMessageDialog(null,"请勿填写空的参数","警告",JOptionPane.WARNING_MESSAGE);
            }

        }
    };
    bt1.addActionListener(actionListener);

}

    public static void main(String[] args) {
        new Insert_user();
    }
}
